import { Component, OnInit, Input } from '@angular/core';
// 导入表单相关：表单验证、表单构建、表单组
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { BaselineService } from 'src/app/core/service/baseline.service';

@Component({
  selector: 'app-add-baseline',
  templateUrl: './add-baseline.component.html',
  styleUrls: ['./add-baseline.component.scss']
})
export class AddBaselineComponent implements OnInit {
  @Input() containerUuid: string;
  baselineForm: FormGroup;
  data = {};

  constructor(
    private modal: NzModalRef,
    private message: NzMessageService,
    private baselineService: BaselineService,
    fb: FormBuilder
  ) {
    this.baselineForm = fb.group({
      baselineName: ['', Validators.required],
      baselineNumber: ['', Validators.required],
      description: [''],
      checkProcess: ['', Validators.required],
      judgementBasis: [''],
      note: [''],
    });
  }

  /* // 执行添加基线的方法
  addRelevance(): void {
    console.log(this.baselineForm.controls);
    // tslint:disable-next-line:forin
    for (const c in this.baselineForm.controls) {
      this.baselineForm.controls[c].markAsTouched();
    }


    if (this.baselineForm.valid) {
      this.data = {
        name: this.baselineForm.value.baselineName,
        baseline_number: this.baselineForm.value.baselineNumber,
        container_ids: [{ 'container_id': '1' }, { 'container_id': '2' }],
        description: this.baselineForm.value.description,
        check_process: this.baselineForm.value.checkProcess,
        judgment_basis: this.baselineForm.value.judgementBasis,
        note: this.baselineForm.value.note,
        modify_time: '2019-03-19 06:03:33',
        create_time: '2019-03-19 06:03:33'
      };
      this.addService(this.data);
    } else {
      this.message.error('表单校验失败', { nzDuration: 10000 });
    }
  }

  private addService(info: object) {
    this.baselineService.addBaseline(info).subscribe((addResult) => {
      // this.result = result;   // 返回的结果
      console.log('==result==', addResult);
      if (addResult.status === 1) {
        this.message.success('添加成功!', {
          nzDuration: 10000
        });
        this.modal.destroy({ data: 'this the result data' });
      } else {
        this.message.error('添加失败!请检查后重新填写', {
          nzDuration: 10000
        });
      }
    });
  } */

  addBaseline() {
    console.log(this.baselineForm.controls);
    // tslint:disable-next-line:forin
    for (const c in this.baselineForm.controls) {
      this.baselineForm.controls[c].markAsTouched();
    }

    if (this.baselineForm.valid) {
      if (this.containerUuid) {
        this.data = {
          name: this.baselineForm.value.baselineName,
          baselineNumber: this.baselineForm.value.baselineNumber,
          containerUuid: this.containerUuid,
          description: this.baselineForm.value.description,
          checkProcess: this.baselineForm.value.checkProcess,
          judgmentBasis: this.baselineForm.value.judgementBasis,
          note: this.baselineForm.value.note
        };
      } else {
        this.data = {
          name: this.baselineForm.value.baselineName,
          baselineNumber: this.baselineForm.value.baselineNumber,
          description: this.baselineForm.value.description,
          checkProcess: this.baselineForm.value.checkProcess,
          judgmentBasis: this.baselineForm.value.judgementBasis,
          note: this.baselineForm.value.note
        };
      }

      this.addBaselineService(this.data);
    } else {
      this.message.error('表单校验失败', { nzDuration: 10000 });
    }
  }

  addBaselineService(info: any) {
    this.baselineService.addBaseline(info).subscribe((addResult) => {
      // this.result = result;   // 返回的结果
      console.log('==result==', addResult);
      if (addResult.status === 1) {
        this.message.success('添加成功!', {
          nzDuration: 10000
        });
        this.modal.destroy({ data: 'this the result data' });
      } else {
        this.message.error('添加失败!请检查后重新填写', {
          nzDuration: 10000
        });
      }
    });
  }


  destroyModal(): void {
    this.modal.destroy({ data: 'this the result data' });
  }




  ngOnInit() {
  }

}
